package cn.zhl.lambda;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-04-20 14:29
 **/
public class BiFunctionTest {
	public static void main(String[] args) {
//        Integer sum = sum(1, 3, (a, b) -> a + b);
//        System.out.println(sum);
		String s = testAddThen(1, 2, (a, b) -> (a + b) * 3, a -> "my age is " + a);
		System.out.println(s);

	}

	static Integer sum(int a, int b, BiFunction<Integer, Integer, Integer> f) {
		return f.apply(a, b);
	}

	static String testAddThen(int a, int b, BiFunction<Integer, Integer, Integer> f1, Function<Integer, String> f2) {
		return f1.andThen(f2).apply(a, b);
	}

	@Test
	public void test() {
		Person person1 = Person.builder().name("小明").age(10).build();
		Person person2 = Person.builder().name("小红").age(20).build();
		Person person3 = Person.builder().name("小亮").age(30).build();

		List<Person> ps = Arrays.asList(person1, person2, person3);
		List<Person> f = f1(10, ps);
		f.stream().forEach(System.out::println);
	}

	public List<Person> f1(int age, List<Person> ps) {
		BiFunction<Integer, List<Person>, List<Person>> bf = (tage, tps) ->
			tps.stream().filter(p -> p.getAge() != 10).collect(Collectors.toList());
		return bf.apply(age, ps);
	}

	@Test
	public void test2() {
		Person person1 = Person.builder().name("小明").age(10).build();
		Person person2 = Person.builder().name("小红").age(20).build();
		Person person3 = Person.builder().name("小亮").age(30).build();

		List<Person> ps = Arrays.asList(person1, person2, person3);
		List<Person> people = f2(10, ps, (age, tps) -> tps.stream().filter(tp -> tp.getAge() != age).collect(Collectors.toList()));
		people.stream().forEach(System.out::println);
	}

	public List<Person> f2(int age, List<Person> ps, BiFunction<Integer, List<Person>, List<Person>> bf) {
		return bf.apply(age, ps);
	}
}
